﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pata.SylkSharp.Core.Bll;

namespace Pata.SylkSharp.Core
{
    /// <summary>
    /// Exception thrown when a mandatory Sylk field is missing
    /// </summary>
    /// <typeparam name="TOfFieldEnum">Type of enumeration that defines fields of this record.</typeparam>
    [Serializable]
    public class MissingSylkFieldException<TOfFieldEnum> : Exception
    {
        /// <summary>
        /// Create a new instance.
        /// </summary>
        /// <param name="pTypeOfRecord">Type of the record that miss a field.</param>
        /// <param name="pMissingField">The missing field.</param>
        public MissingSylkFieldException(RecordType pTypeOfRecord, TOfFieldEnum pMissingField)
            : base(String.Format("The field {0} for record {1} is missing", pMissingField, pTypeOfRecord))
        {

        }

        /// <summary>
        /// Create a new instance.
        /// </summary>
        /// <param name="pTypeOfRecord">Type of the record that miss a field.</param>
        /// <param name="pMissingFieldEnum">The missing fields.</param>
        public MissingSylkFieldException(RecordType pTypeOfRecord, IEnumerable<TOfFieldEnum> pMissingFieldEnum)
            : base(String.Format("The fields : {0} for record {1} are missing", ConcatenateWithComma(pMissingFieldEnum), pTypeOfRecord))
        {

        }

        private static string ConcatenateWithComma(IEnumerable<TOfFieldEnum> fieldToAggregate)
        {
            return String.Join(", ", fieldToAggregate.Select(t => t.ToString()).ToArray());
        }
    }
}
